package com.ukao.cashregister.ui.login;


import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.bean.AccountLoginBean;
import com.ukao.cashregister.bean.TokenBean;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.eventbus.MembershipCardEvent;
import com.ukao.cashregister.eventbus.ReadCardEvent;
import com.ukao.cashregister.pesenter.SwipingCardLoginPesenter;
import com.ukao.cashregister.rfid.MembershipCardHelper;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.SwipingCardLoginView;

import org.greenrobot.eventbus.Subscribe;

import static com.ukao.cashregister.consts.Constant.loginSucceedJump;

/**
刷卡登录
 */
public class SwipingCardragment extends MvpFragment<SwipingCardLoginPesenter>  implements SwipingCardLoginView{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;
    private Handler mHandler = new Handler();
    private String rfidSecretCard;

    private  boolean isNfcReadCard=false;
    public static SwipingCardragment newInstance(String param1, String param2) {
        SwipingCardragment fragment = new SwipingCardragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected SwipingCardLoginPesenter createPresenter() {
        return new SwipingCardLoginPesenter(this);
    }

    @Override
    protected void initView() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_swiping_cardragment, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isNfcReadCard=true;
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        isVisible=false;

        mHandler.removeCallbacksAndMessages(null);

    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        isVisible=true;
        isNfcReadCard=true;
        postDelayedReadCard();


    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);

        postDelayedReadCard();
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }


    @Subscribe
    public void onEvent(MembershipCardEvent event) {
        L.i("isVisible="+isVisible);
        switch (event.getMessage()) {
            case add:/*成功*/
                if (!isVisible){
                    return;
                }

                rfidSecretCard=(String)event.getData();
                mvpPresenter.appSecretData();

                break;

            case fali:/*读卡失败*/
                if (!isVisible){
                    return;
                }
                String  error=(String)event.getData();
                T.show(error);
                postDelayedReadCard();
                break;
            case notFound:/*没有卡*/
                if (!isVisible){
                    return;
                }
                postDelayedReadCard();

                break;

            case   nfc_read:  /*商米NFC读卡*/
                if (!isNfcReadCard)return;
                rfidSecretCard=(String)event.getData();
                mvpPresenter.appSecretData();
                break;



        }
    }




    /**
     * 读卡
     */
    private void postDelayedReadCard() {
        mHandler.removeCallbacksAndMessages(null);
        if (isVisible){
            mHandler.postDelayed(() -> {

                L.i("读卡");
                MembershipCardHelper.getInstance().readUserId();

            }, 1300);
        }


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        MembershipCardHelper.getInstance().onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }



    @Override
    public void loadloginSucceed(AccountLoginBean mitem) {

        startActivity(loginSucceedJump(_mActivity,mitem));
        getActivity().finish();

    }


    @Override
    public void loadFailure(String message) {
        T.show(message);
        postDelayedReadCard();

    }

    @Override
    public void appSecretSucceed(TokenBean mTokenBean) {

        SaveParamets.saveloginSign(Utils.getContext(), mTokenBean.getData().getAppSecret());
        mvpPresenter.swipingCardlogin(rfidSecretCard);
    }
}
